function obj_eval = stack_solver_inner(param,fp,vp,return_u,gamma,t)

% This file solves the general period t household problem

% Initial state variables:
% k = stock of cognitive human capital
% p = stock of patience capital
% w1 = mother's hourly wage offer
% w2 = father's hourly wage offer
% I = non-labor income

% First, condition on the following endogenous choices
% CCT = 0 or 1, parents using an ICCT or not
% tau1 = mother's investment time
% tau2 = father's investment time
% r = ICCT reward elasticity (if CCT = 1, otherwise r = 0)

% First define child's time endowment at age t+1; don't forget to shift the index one up, since we
% observe ages t = 0-fp.M (i.e. 0-16), so e.g. at age 12, we need the 13th entry from fp.TT_c
T_tilde = fp.TT_c(t+1); % effective time endowment at this age

% Constrain the parameters and define closed form solutions
% 1) Constrain total parental time (1st parameter)
taup = (exp(param(1))/(1+exp(param(1)))) * T_tilde; % this will ensure that tauc > = 0
% 2) Constrain mother's investment time (2nd parameter)
tau1 = (exp(param(2))/(1+exp(param(2)))) * taup;
tau2 = taup - tau1; % father's investment time

% Define useful scalar:
Deltap4 = vp.alpha3_tilde + vp.alpha6_tilde + vp.beta_p * vp.delta4(t) * vp.psi_p_tp1;

% Closed form solutions for expenditures/leisure/labor (if labor is interior)
tmp_ratio = (vp.w1*(fp.TT_p-tau1) + vp.w2*(fp.TT_p-tau2) + vp.I)/(vp.alpha1_tilde + vp.alpha2_tilde + Deltap4);
l1 = tmp_ratio * vp.alpha1_tilde / vp.w1;
l2 = tmp_ratio * vp.alpha2_tilde / vp.w2;
h1 = fp.TT_p-tau1-l1;
h2 = fp.TT_p-tau2-l2;

% Redefine solutions if we are at a corner for labor supply
if h1 < 0 && h2 >= 0
    % Corner 2: mother's labor supply has to be reset to 0
    h1 = 0;
    l1 = fp.TT_p - tau1;
    l2 = (vp.alpha2_tilde*(vp.w2*(fp.TT_p-tau2) + vp.I))/(vp.w2*(vp.alpha2_tilde + Deltap4));
    h2 = fp.TT_p-tau2-l2;
    % NOTE: have to verify (below) whether h2>=0
elseif h2 < 0 && h1 >= 0
    % Corner 3: father's labor supply has to be reset to 0
    h2 = 0;
    l2 = fp.TT_p - tau2;
    l1 = (vp.alpha1_tilde*(vp.w1*(fp.TT_p-tau1) + vp.I))/(vp.w1*(vp.alpha1_tilde + Deltap4));
    h1 = fp.TT_p-tau1-l1;
    % NOTE: have to verify (below) whether h1>=0
end

if h1 < 0 || h2 < 0 %Note the OR here to adjust previous 2 corners if needed
    % Corner 4: both spouses supply 0 hours
    h1=0;
    l1=fp.TT_p-tau1;
    h2=0;
    l2=fp.TT_p-tau2;
end

% Define total income and expenditures
Y = vp.w1*h1 + vp.w2*h2 + vp.I;
% closed form solutions after substituting out x (where e is "unconstrained"!)
x = Y * vp.alpha6_tilde / Deltap4;
c = Y * vp.alpha3_tilde / Deltap4;
e = Y - c - x; 

tauc = (T_tilde-taup)*gamma;
lc = T_tilde-tauc-taup; 

% Child cognitive skills NOT INCLUDING EFFECT OF CURRENT k_t
log_k_tp1 = log(vp.R(t)) + vp.delta1(t)*log(tau1+eps) + vp.delta2(t)*log(tau2+eps) + vp.delta3(t)*log(tauc+eps) + vp.delta4(t)*log(e+eps);

% Bellman equation (NOTE: this does NOT include utility cost of CCT!)
% And also doesn't include any term that multiplies log(k_t)
V_p = vp.alpha1_tilde*log(l1+eps) + vp.alpha2_tilde*log(l2+eps) + vp.alpha3_tilde*log(c+eps) + vp.alpha5_tilde*log(lc+eps) + vp.alpha6_tilde*log(x+eps) + vp.beta_p * vp.psi_p_tp1 * log_k_tp1;
V_p(~isfinite(V_p)) = -1e10; % safety switch: assign a very negative but real number if V_p = -Inf or NaN, so the solver can continue

%function output;
if return_u == 1
    obj_eval = -V_p; % because we want to maximize V_p, i.e. minimize -V_p
    return
end

% Child's value given initial state (k,p) and choices (CCT,tau1,tau2)
% Again, NOT INCLUDING EFFECT OF INITIAL k_t
V_c = vp.lambda1*log(lc) + vp.lambda2*log(x) + vp.beta_ct * vp.psi_c_tp1 * log_k_tp1;

if return_u == 0
    obj_eval.V_p = V_p; % % INCOMPLETE bellman fct, not utility or welfare
    obj_eval.log_k_tp1 = log_k_tp1; % INCOMPLETE
    obj_eval.tau1 = tau1; % mom time
    obj_eval.tau2 = tau2; % dad time
    obj_eval.tauc = tauc; % child self-investment time
    obj_eval.h1 = h1; % mom hours
    obj_eval.h2 = h2; % dad hours
    obj_eval.l1 = l1; % mom leisure
    obj_eval.l2 = l2; % dad leisure
    obj_eval.lc = lc; % child leisure
    obj_eval.e = e; % child expenditures
    obj_eval.c = c; % parental consumption
    obj_eval.x = x; % child consumption
    obj_eval.Y = Y; % total income
    obj_eval.b = log(x)-vp.r*log(tauc); % CCT intercept;
    obj_eval.r = vp.r; % CCT slope
    obj_eval.V_c = V_c; % INCOMPLETE - does not include all terms, add them in in sim_funct.m
    % Instantaneous utilities
    % Parents' instantaneous altruistic utility (incomplete)
    obj_eval.u_p_tilde = vp.alpha1_tilde*log(l1+eps) + vp.alpha2_tilde*log(l2+eps) + vp.alpha3_tilde*log(c+eps) + vp.alpha5_tilde*log(lc+eps) + vp.alpha6_tilde*log(x+eps); 
    % Child's instantaneous utility (incomplete)
    obj_eval.u_c = vp.lambda1*log(lc) + vp.lambda2*log(x); 
end

end % function